import 'dart:ffi';

import 'package:my_flutter_demo/exports.dart';

class TextFieldDemoPage extends StatefulWidget {
  const TextFieldDemoPage({super.key});

  @override
  State<TextFieldDemoPage> createState() => TextFieldDemoPageState();
}

class TextFieldDemoPageState extends State<TextFieldDemoPage>{

  final TextEditingController _controller = TextEditingController(text: '初始文字');
  final FocusNode _focusNode = FocusNode();
  bool _isFocus = false ;

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TextField'),
      ),
      body: Center(
        child: Column(
          children: [
            TextField(
              controller: _controller,
              onChanged: (value){
                print(value);
              },
              focusNode: _focusNode,
              onTap: (){
                print('点击了');
              },
              onEditingComplete: (){
                print('编辑完成');
              },
                onSubmitted: (value) => print('提交内容: $value'),
                onTapOutside: (event) => print('点击了外部区域')
            ),
            SizedBox(
              height: 20,
            ),
            TextField(
              decoration: InputDecoration(
                labelText: 'username',
                hintText: 'Enter your username',
                border: OutlineInputBorder(),
                prefixIcon: Icon(Icons.person),
              ),
              keyboardType: TextInputType.text,
              onChanged: (value){
                print(value);
              },
            ),
            SizedBox(
              height: 20,
            ),
            TextField(
              maxLength: 10,
              obscureText: true,
              decoration: InputDecoration(
                hintText: 'Enter your password',
                prefixIcon: Icon(Icons.abc),
                border: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.green)
                ),
                // enabledBorder: OutlineInputBorder(
                //   borderSide: BorderSide(color: Colors.orange)
                // ),
                // focusedBorder: OutlineInputBorder(
                //     borderSide: BorderSide(color: Colors.orange),
                //     borderRadius: BorderRadius.circular(10)
                // )
              ),
              keyboardType: TextInputType.text,
              onChanged: (value){
                print(value);
              },
            ),
            SizedBox(
              height: 50,
            ),
            TextButton(
                onPressed: (){
                  if (_isFocus){
                    _focusNode.unfocus();
                  }else{
                    _focusNode.requestFocus();
                  }
                  _isFocus = !_isFocus;
                },
                child: Text('焦点控制'))
          ],
        ),
      ),
    );
  }

}